//
//  IntroductionView.m
//  ZhenPin
//
//  Created by fei on 14-7-9.
//  Copyright (c) 2014年 zhenpin. All rights reserved.
//

#import "IntroductionView.h"
#import "PanelView.h"


@implementation IntroductionView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}
-(id)initWithFrame:(CGRect)frame andIntroductionViewsArray:(NSArray *)viewArray {
    if (self = [super initWithFrame:frame]) {
        [self setIntroductionViewsArray:viewArray];
    }
    return self;
}
-(void)setIntroductionViewsArray:(NSArray *)viewArray {
    introductViewArray = [NSMutableArray arrayWithArray:viewArray];
    
    if (viewArray.count > 0) {
        backScrollView = [[UIScrollView alloc] initWithFrame:self.frame];
        backScrollView.pagingEnabled = YES;
        backScrollView.delaysContentTouches = NO;
        backScrollView.showsHorizontalScrollIndicator = NO;
        backScrollView.showsVerticalScrollIndicator = NO;
        backScrollView.delegate = self;
        for (int i = 0; i < viewArray.count; i ++) {
            PanelView *customView = viewArray[i];
            customView.frame = CGRectMake(self.frame.size.width*i, 0, self.frame.size.width, self.frame.size.height);
            customView.panelDelegate = self;
            
            [backScrollView addSubview:customView];
        }
        backScrollView.contentSize = CGSizeMake(viewArray.count*self.frame.size.width, self.frame.size.height);
        [self addSubview:backScrollView];
    } else {
        //DO:(@"introduction subviews can not be nil");
    }
}

-(void)hideAndFadeOutFromSuperView {
    
    [UIView animateWithDuration:0.8 animations:^{
        self.alpha = 0;
    } completion:^(BOOL isFinished) {
        if (isFinished) {
            [introductViewArray removeAllObjects];
            [self removeFromSuperview];
        }
    }];
}


#pragma  mark -- UIScrollView Delegate 
-(void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if (scrollView.contentOffset.x >= backScrollView.contentSize.width-backScrollView.frame.size.width) {
        backScrollView.bounces = NO;
    } else {
        backScrollView.bounces = YES;
    }
    
}
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    currentOffset_x = scrollView.contentOffset.x/backScrollView.frame.size.width;
    if (scrollView.contentOffset.x >= backScrollView.contentSize.width-backScrollView.frame.size.width) {
        backScrollView.bounces = NO;
    } else {
        backScrollView.bounces = YES;
    }
}
#pragma  mark -- panelView Delegate
-(void)completeShowAndHidden {
    NSUserDefaults *userD = [NSUserDefaults standardUserDefaults];
    [userD setObject:[NSNumber numberWithBool:YES] forKey:@"isFirst"];
    [self hideAndFadeOutFromSuperView];
}

@end
